Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

279
Vistas
Are they the "same"? Codewars Kata

I'm trying to solve the above-mentioned codewars kata with the following instructions:

Given two arrays a and b write a function comp(a, b) (orcompSame(a, b)) that checks whether the two arrays have the "same" elements, with the same multiplicities (the multiplicity of a member is the number of times it appears). "Same" means, here, that the elements in b are the elements in a squared, regardless of the order.

Examples Valid arrays a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [121, 14641, 20736, 361, 25921, 361, 20736, 361] comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b's elements in terms of squares:

a = [121, 144, 19, 161, 19, 144, 19, 11] b = [1111, 121121, 144144, 1919, 161161, 1919, 144144, 1919] Invalid arrays If, for example, we change the first number to something else, comp is not returning true anymore:

a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [132, 14641, 20736, 361, 25921, 361, 20736, 361] comp(a,b) returns false because in b 132 is not the square of any number of a.

a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361] comp(a,b) returns false because in b 36100 is not the square of any number of a.

Remarks a or b might be [] or {} (all languages except R, Shell). a or b might be nil or null or None or nothing (except in C++, Crystal, D, Dart, Elixir, Fortran, F#, Haskell, Nim, OCaml, Pascal, Perl, PowerShell, Prolog, PureScript, R, Racket, Rust, Shell, Swift). If a or b are nil (or null or None, depending on the language), the problem doesn't make sense so return false.

This is my code, which is passing the initial tests but failing some tests later:

const comp = (a, b) => {
    function sortNumber(n1, n2) {
        return n1 - n2;
    }
    if (a === [] || b === [] || a === {} || b === {} || a === null || b === null) {
        return false
    } else {
        a = a.sort(sortNumber)
        b = b.sort(sortNumber)
    }
    for (let i = 0; i < a.length; i++) {
        for (let i = 0; i < b.length; i++) {
            if (Math.pow(a[i], 2) === b[i]) {
                return true
            } else {
                return false
            }

        }
    }
}

The error message I'm getting is: It should work with random inputs too - expected false but got true (or viceversa). What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda